{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "82599b56",
   "metadata": {},
   "source": [
    "# Pandas 查询数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79a2f86d",
   "metadata": {},
   "source": [
    "## Pandas查询数据的几种方法\n",
    "1.df.loc方法，根据行、列的标签值查询<br>\n",
    "2.df.iloc方法，根据行列的数字位置查询<br>\n",
    "3.df.where方法<br>\n",
    "4.df.query方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "162b204e",
   "metadata": {},
   "source": [
    "loc既能查询，又能覆盖写入，强烈推荐！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5053d5ca",
   "metadata": {},
   "source": [
    "## Pandas使用df.loc查询数据的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8f58793",
   "metadata": {},
   "source": [
    "1.使用单个`label`值查询数据<br>\n",
    "2.使用值列表批量查询<br>\n",
    "3.使用数值区间进行范围查询<br>\n",
    "4.使用条件表达式查询<br>\n",
    "5.调用函数查询"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3432dfc3",
   "metadata": {},
   "source": [
    "# 注意\n",
    "* 以上查询方法，既适用于行，也适用于列\n",
    "* 注意观察降维`dataFrame`->`Series`->值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6a909927",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b491f46d",
   "metadata": {},
   "source": [
    "## 0. 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "c7ecca78",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"datas/weather_20230115134249.csv\", encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "3b0e27b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>城市</th>\n",
       "      <th>行政区</th>\n",
       "      <th>观测站</th>\n",
       "      <th>气温(度)</th>\n",
       "      <th>相对湿度(%)</th>\n",
       "      <th>累积雨量(mm)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2015-1-1</td>\n",
       "      <td>新北市</td>\n",
       "      <td>烏來區</td>\n",
       "      <td>福山</td>\n",
       "      <td>13.7℃</td>\n",
       "      <td>92</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2015-1-2</td>\n",
       "      <td>臺南市</td>\n",
       "      <td>安平區</td>\n",
       "      <td>安平</td>\n",
       "      <td>23.5℃</td>\n",
       "      <td>70</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2015-1-3</td>\n",
       "      <td>臺東縣</td>\n",
       "      <td>東河鄉</td>\n",
       "      <td>七塊厝</td>\n",
       "      <td>19.6℃</td>\n",
       "      <td>86</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015-1-4</td>\n",
       "      <td>新北市</td>\n",
       "      <td>貢寮區</td>\n",
       "      <td>福隆</td>\n",
       "      <td>14.2℃</td>\n",
       "      <td>96</td>\n",
       "      <td>-99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2015-1-5</td>\n",
       "      <td>南投縣</td>\n",
       "      <td>仁愛鄉</td>\n",
       "      <td>小奇萊</td>\n",
       "      <td>8.3℃</td>\n",
       "      <td>57</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         日期   城市  行政区  观测站  气温(度)  相对湿度(%)  累积雨量(mm)\n",
       "0  2015-1-1  新北市  烏來區   福山  13.7℃       92       0.0\n",
       "1  2015-1-2  臺南市  安平區   安平  23.5℃       70       0.0\n",
       "2  2015-1-3  臺東縣  東河鄉  七塊厝  19.6℃       86       0.0\n",
       "3  2015-1-4  新北市  貢寮區   福隆  14.2℃       96     -99.0\n",
       "4  2015-1-5  南投縣  仁愛鄉  小奇萊   8.3℃       57       0.0"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "ec5cfef4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设定索引为日期，方便日期筛选\n",
    "df.set_index('日期',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "76d9a271",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['2015-1-1', '2015-1-2', '2015-1-3', '2015-1-4', '2015-1-5', '2015-1-6',\n",
       "       '2015-1-7', '2015-1-8', '2015-1-9', '2015-1-10',\n",
       "       ...\n",
       "       '2016-4-19', '2016-4-20', '2016-4-21', '2016-4-22', '2016-4-23',\n",
       "       '2016-4-24', '2016-4-25', '2016-4-26', '2016-4-27', '2016-4-28'],\n",
       "      dtype='object', name='日期', length=484)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "f4c71f2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc[:,\"气温(度)\"]=df[\"气温(度)\"].str.replace(\"℃\",\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7064704b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "城市           object\n",
       "行政区          object\n",
       "观测站          object\n",
       "气温(度)         int32\n",
       "相对湿度(%)       int64\n",
       "累积雨量(mm)    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "3e11ea53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>城市</th>\n",
       "      <th>行政区</th>\n",
       "      <th>观测站</th>\n",
       "      <th>气温(度)</th>\n",
       "      <th>相对湿度(%)</th>\n",
       "      <th>累积雨量(mm)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-1-1</th>\n",
       "      <td>新北市</td>\n",
       "      <td>烏來區</td>\n",
       "      <td>福山</td>\n",
       "      <td>13.7</td>\n",
       "      <td>92</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-2</th>\n",
       "      <td>臺南市</td>\n",
       "      <td>安平區</td>\n",
       "      <td>安平</td>\n",
       "      <td>23.5</td>\n",
       "      <td>70</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-3</th>\n",
       "      <td>臺東縣</td>\n",
       "      <td>東河鄉</td>\n",
       "      <td>七塊厝</td>\n",
       "      <td>19.6</td>\n",
       "      <td>86</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-4</th>\n",
       "      <td>新北市</td>\n",
       "      <td>貢寮區</td>\n",
       "      <td>福隆</td>\n",
       "      <td>14.2</td>\n",
       "      <td>96</td>\n",
       "      <td>-99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-5</th>\n",
       "      <td>南投縣</td>\n",
       "      <td>仁愛鄉</td>\n",
       "      <td>小奇萊</td>\n",
       "      <td>8.3</td>\n",
       "      <td>57</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           城市  行政区  观测站 气温(度)  相对湿度(%)  累积雨量(mm)\n",
       "日期                                              \n",
       "2015-1-1  新北市  烏來區   福山  13.7       92       0.0\n",
       "2015-1-2  臺南市  安平區   安平  23.5       70       0.0\n",
       "2015-1-3  臺東縣  東河鄉  七塊厝  19.6       86       0.0\n",
       "2015-1-4  新北市  貢寮區   福隆  14.2       96     -99.0\n",
       "2015-1-5  南投縣  仁愛鄉  小奇萊   8.3       57       0.0"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe1adb08",
   "metadata": {},
   "source": [
    "## 1. 使用单个label值查询数据\n",
    "行或者列，都可以只传入单个值，实现精确匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "902ebbe0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'13.5'"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 得到单个值\n",
    "df.loc['2015-5-12','气温(度)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "2752fb9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "气温(度)      13.5\n",
       "相对湿度(%)      81\n",
       "Name: 2015-5-12, dtype: object"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 得到一个Series\n",
    "df.loc['2015-5-12',['气温(度)','相对湿度(%)']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abaeb91d",
   "metadata": {},
   "source": [
    "## 2. 使用值列表批量查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "09b77d74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "日期\n",
       "2015-5-12     13.5\n",
       "2015-10-10    22.1\n",
       "Name: 气温(度), dtype: object"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 得到Series\n",
    "df.loc[['2015-5-12','2015-10-10'],'气温(度)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "4fbbfb90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>气温(度)</th>\n",
       "      <th>相对湿度(%)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-5-12</th>\n",
       "      <td>13.5</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-10</th>\n",
       "      <td>22.1</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           气温(度)  相对湿度(%)\n",
       "日期                       \n",
       "2015-5-12   13.5       81\n",
       "2015-10-10  22.1       60"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 得到DataFrame\n",
    "df.loc[['2015-5-12','2015-10-10'],['气温(度)','相对湿度(%)']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf34ded9",
   "metadata": {},
   "source": [
    "## 3. 使用数值区间进行范围查询\n",
    "注意：区间既包含开始，也包含结束"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "6f6b451e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "日期\n",
       "2015-5-12    13.5\n",
       "2015-5-13     -99\n",
       "Name: 气温(度), dtype: object"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行index按区间\n",
    "df.loc['2015-5-12':'2015-5-13','气温(度)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "8f0a85c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "气温(度)       22.1\n",
       "相对湿度(%)       60\n",
       "累积雨量(mm)     0.0\n",
       "Name: 2015-10-10, dtype: object"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列index按区间\n",
    "df.loc['2015-10-10','气温(度)':'累积雨量(mm)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "531e5023",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>气温(度)</th>\n",
       "      <th>相对湿度(%)</th>\n",
       "      <th>累积雨量(mm)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-5-12</th>\n",
       "      <td>13.5</td>\n",
       "      <td>81</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-5-13</th>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          气温(度)  相对湿度(%)  累积雨量(mm)\n",
       "日期                                \n",
       "2015-5-12  13.5       81       6.0\n",
       "2015-5-13   -99    -9900       0.0"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行和列都按区间查询\n",
    "df.loc['2015-5-12':'2015-5-13','气温(度)':'累积雨量(mm)']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae9e0f91",
   "metadata": {},
   "source": [
    "## 4. 使用条件表达式查询\n",
    "boolean列表的长度得等于行数或者列数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d481cdc1",
   "metadata": {},
   "source": [
    "**简单条件查询，最低温度低于-10度的列表**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "a7ead587",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>城市</th>\n",
       "      <th>行政区</th>\n",
       "      <th>观测站</th>\n",
       "      <th>气温(度)</th>\n",
       "      <th>相对湿度(%)</th>\n",
       "      <th>累积雨量(mm)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-1-16</th>\n",
       "      <td>苗栗縣</td>\n",
       "      <td>造橋鄉</td>\n",
       "      <td>造橋</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>-99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-25</th>\n",
       "      <td>臺中市</td>\n",
       "      <td>西屯區</td>\n",
       "      <td>西屯</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>-99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-2-22</th>\n",
       "      <td>南投縣</td>\n",
       "      <td>仁愛鄉</td>\n",
       "      <td>奇萊稜線A</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-5-13</th>\n",
       "      <td>臺南市</td>\n",
       "      <td>西港區</td>\n",
       "      <td>西港</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-6-8</th>\n",
       "      <td>屏東縣</td>\n",
       "      <td>新埤鄉</td>\n",
       "      <td>新埤</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>-99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-6-11</th>\n",
       "      <td>新北市</td>\n",
       "      <td>瑞芳區</td>\n",
       "      <td>鼻頭角</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-6-12</th>\n",
       "      <td>臺中市</td>\n",
       "      <td>烏日區</td>\n",
       "      <td>烏日</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>-99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-7-6</th>\n",
       "      <td>新北市</td>\n",
       "      <td>汐止區</td>\n",
       "      <td>汐止</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>-99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-1-17</th>\n",
       "      <td>高雄市</td>\n",
       "      <td>梓官區</td>\n",
       "      <td>梓官</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>-99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-3-29</th>\n",
       "      <td>屏東縣</td>\n",
       "      <td>長治鄉</td>\n",
       "      <td>長治</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-15</th>\n",
       "      <td>新北市</td>\n",
       "      <td>永和區</td>\n",
       "      <td>永和</td>\n",
       "      <td>-99</td>\n",
       "      <td>-9900</td>\n",
       "      <td>-99.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            城市  行政区    观测站 气温(度)  相对湿度(%)  累积雨量(mm)\n",
       "日期                                                 \n",
       "2015-1-16  苗栗縣  造橋鄉     造橋   -99    -9900     -99.0\n",
       "2015-1-25  臺中市  西屯區     西屯   -99    -9900     -99.0\n",
       "2015-2-22  南投縣  仁愛鄉  奇萊稜線A   -99    -9900       0.0\n",
       "2015-5-13  臺南市  西港區     西港   -99    -9900       0.0\n",
       "2015-6-8   屏東縣  新埤鄉     新埤   -99    -9900     -99.0\n",
       "2015-6-11  新北市  瑞芳區    鼻頭角   -99    -9900       0.5\n",
       "2015-6-12  臺中市  烏日區     烏日   -99    -9900     -99.0\n",
       "2015-7-6   新北市  汐止區     汐止   -99    -9900     -99.0\n",
       "2016-1-17  高雄市  梓官區     梓官   -99    -9900     -99.0\n",
       "2016-3-29  屏東縣  長治鄉     長治   -99    -9900       0.0\n",
       "2016-4-15  新北市  永和區     永和   -99    -9900     -99.0"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[df[\"气温(度)\"].astype(float)<-10,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "47d89c3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "日期\n",
       "2015-1-1     False\n",
       "2015-1-2     False\n",
       "2015-1-3     False\n",
       "2015-1-4     False\n",
       "2015-1-5     False\n",
       "             ...  \n",
       "2016-4-24    False\n",
       "2016-4-25    False\n",
       "2016-4-26    False\n",
       "2016-4-27    False\n",
       "2016-4-28    False\n",
       "Name: 气温(度), Length: 484, dtype: bool"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 观察一些这里的boolean条件\n",
    "df[\"气温(度)\"].astype(float)<-10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40d68a2b",
   "metadata": {},
   "source": [
    "**复杂查询条件，查一下我心中的目标天气**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "7f418326",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>城市</th>\n",
       "      <th>行政区</th>\n",
       "      <th>观测站</th>\n",
       "      <th>气温(度)</th>\n",
       "      <th>相对湿度(%)</th>\n",
       "      <th>累积雨量(mm)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-1-2</th>\n",
       "      <td>臺南市</td>\n",
       "      <td>安平區</td>\n",
       "      <td>安平</td>\n",
       "      <td>23.5</td>\n",
       "      <td>70</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-3</th>\n",
       "      <td>臺東縣</td>\n",
       "      <td>東河鄉</td>\n",
       "      <td>七塊厝</td>\n",
       "      <td>19.6</td>\n",
       "      <td>86</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-6</th>\n",
       "      <td>嘉義縣</td>\n",
       "      <td>大林鎮</td>\n",
       "      <td>大林</td>\n",
       "      <td>23.2</td>\n",
       "      <td>63</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-7</th>\n",
       "      <td>花蓮縣</td>\n",
       "      <td>玉里鎮</td>\n",
       "      <td>玉里</td>\n",
       "      <td>18.5</td>\n",
       "      <td>85</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-8</th>\n",
       "      <td>嘉義市</td>\n",
       "      <td>東區</td>\n",
       "      <td>嘉義市東區</td>\n",
       "      <td>25</td>\n",
       "      <td>64</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-22</th>\n",
       "      <td>新北市</td>\n",
       "      <td>三重區</td>\n",
       "      <td>三重</td>\n",
       "      <td>16.3</td>\n",
       "      <td>76</td>\n",
       "      <td>3.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-23</th>\n",
       "      <td>高雄市</td>\n",
       "      <td>茄萣區</td>\n",
       "      <td>興達</td>\n",
       "      <td>25.1</td>\n",
       "      <td>67</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-24</th>\n",
       "      <td>新北市</td>\n",
       "      <td>板橋區</td>\n",
       "      <td>板橋</td>\n",
       "      <td>15.6</td>\n",
       "      <td>73</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-26</th>\n",
       "      <td>花蓮縣</td>\n",
       "      <td>壽豐鄉</td>\n",
       "      <td>大坑</td>\n",
       "      <td>15.7</td>\n",
       "      <td>94</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-27</th>\n",
       "      <td>新竹縣</td>\n",
       "      <td>五峰鄉</td>\n",
       "      <td>雪霸</td>\n",
       "      <td>15.7</td>\n",
       "      <td>62</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>367 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            城市  行政区    观测站 气温(度)  相对湿度(%)  累积雨量(mm)\n",
       "日期                                                 \n",
       "2015-1-2   臺南市  安平區     安平  23.5       70       0.0\n",
       "2015-1-3   臺東縣  東河鄉    七塊厝  19.6       86       0.0\n",
       "2015-1-6   嘉義縣  大林鎮     大林  23.2       63       0.0\n",
       "2015-1-7   花蓮縣  玉里鎮     玉里  18.5       85       0.0\n",
       "2015-1-8   嘉義市   東區  嘉義市東區    25       64       0.0\n",
       "...        ...  ...    ...   ...      ...       ...\n",
       "2016-4-22  新北市  三重區     三重  16.3       76       3.5\n",
       "2016-4-23  高雄市  茄萣區     興達  25.1       67       0.0\n",
       "2016-4-24  新北市  板橋區     板橋  15.6       73       5.0\n",
       "2016-4-26  花蓮縣  壽豐鄉     大坑  15.7       94       0.0\n",
       "2016-4-27  新竹縣  五峰鄉     雪霸  15.7       62       0.0\n",
       "\n",
       "[367 rows x 6 columns]"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询最高温度小于30度，最低温度大于15度的天气数据\n",
    "df.loc[(df[\"气温(度)\"].astype(float)<30) & (df[\"气温(度)\"].astype(float)>15),:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "a4c8a328",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "日期\n",
       "2015-1-1     False\n",
       "2015-1-2      True\n",
       "2015-1-3      True\n",
       "2015-1-4     False\n",
       "2015-1-5     False\n",
       "             ...  \n",
       "2016-4-24     True\n",
       "2016-4-25    False\n",
       "2016-4-26     True\n",
       "2016-4-27     True\n",
       "2016-4-28    False\n",
       "Name: 气温(度), Length: 484, dtype: bool"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 再次观察这里的boolean条件\n",
    "(df[\"气温(度)\"].astype(float)<30) & (df[\"气温(度)\"].astype(float)>15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca9586c8",
   "metadata": {},
   "source": [
    "## 5. 调用函数查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "e81aabb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>城市</th>\n",
       "      <th>行政区</th>\n",
       "      <th>观测站</th>\n",
       "      <th>气温(度)</th>\n",
       "      <th>相对湿度(%)</th>\n",
       "      <th>累积雨量(mm)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-1-2</th>\n",
       "      <td>臺南市</td>\n",
       "      <td>安平區</td>\n",
       "      <td>安平</td>\n",
       "      <td>23.5</td>\n",
       "      <td>70</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-3</th>\n",
       "      <td>臺東縣</td>\n",
       "      <td>東河鄉</td>\n",
       "      <td>七塊厝</td>\n",
       "      <td>19.6</td>\n",
       "      <td>86</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-6</th>\n",
       "      <td>嘉義縣</td>\n",
       "      <td>大林鎮</td>\n",
       "      <td>大林</td>\n",
       "      <td>23.2</td>\n",
       "      <td>63</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-7</th>\n",
       "      <td>花蓮縣</td>\n",
       "      <td>玉里鎮</td>\n",
       "      <td>玉里</td>\n",
       "      <td>18.5</td>\n",
       "      <td>85</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-1-8</th>\n",
       "      <td>嘉義市</td>\n",
       "      <td>東區</td>\n",
       "      <td>嘉義市東區</td>\n",
       "      <td>25</td>\n",
       "      <td>64</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-22</th>\n",
       "      <td>新北市</td>\n",
       "      <td>三重區</td>\n",
       "      <td>三重</td>\n",
       "      <td>16.3</td>\n",
       "      <td>76</td>\n",
       "      <td>3.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-23</th>\n",
       "      <td>高雄市</td>\n",
       "      <td>茄萣區</td>\n",
       "      <td>興達</td>\n",
       "      <td>25.1</td>\n",
       "      <td>67</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-24</th>\n",
       "      <td>新北市</td>\n",
       "      <td>板橋區</td>\n",
       "      <td>板橋</td>\n",
       "      <td>15.6</td>\n",
       "      <td>73</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-26</th>\n",
       "      <td>花蓮縣</td>\n",
       "      <td>壽豐鄉</td>\n",
       "      <td>大坑</td>\n",
       "      <td>15.7</td>\n",
       "      <td>94</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-4-27</th>\n",
       "      <td>新竹縣</td>\n",
       "      <td>五峰鄉</td>\n",
       "      <td>雪霸</td>\n",
       "      <td>15.7</td>\n",
       "      <td>62</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>367 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            城市  行政区    观测站 气温(度)  相对湿度(%)  累积雨量(mm)\n",
       "日期                                                 \n",
       "2015-1-2   臺南市  安平區     安平  23.5       70       0.0\n",
       "2015-1-3   臺東縣  東河鄉    七塊厝  19.6       86       0.0\n",
       "2015-1-6   嘉義縣  大林鎮     大林  23.2       63       0.0\n",
       "2015-1-7   花蓮縣  玉里鎮     玉里  18.5       85       0.0\n",
       "2015-1-8   嘉義市   東區  嘉義市東區    25       64       0.0\n",
       "...        ...  ...    ...   ...      ...       ...\n",
       "2016-4-22  新北市  三重區     三重  16.3       76       3.5\n",
       "2016-4-23  高雄市  茄萣區     興達  25.1       67       0.0\n",
       "2016-4-24  新北市  板橋區     板橋  15.6       73       5.0\n",
       "2016-4-26  花蓮縣  壽豐鄉     大坑  15.7       94       0.0\n",
       "2016-4-27  新竹縣  五峰鄉     雪霸  15.7       62       0.0\n",
       "\n",
       "[367 rows x 6 columns]"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接写lambda表达式\n",
    "df.loc[lambda df : (df[\"气温(度)\"].astype(float)<30) & (df[\"气温(度)\"].astype(float)>15),:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "7c9da391",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "日期\n",
       "2015-1-1      True\n",
       "2015-1-2      True\n",
       "2015-1-3      True\n",
       "2015-1-4     False\n",
       "2015-1-5      True\n",
       "             ...  \n",
       "2016-4-24    False\n",
       "2016-4-25     True\n",
       "2016-4-26     True\n",
       "2016-4-27     True\n",
       "2016-4-28    False\n",
       "Name: 累积雨量(mm), Length: 484, dtype: bool"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"累积雨量(mm)\"]==0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "525b9cd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>城市</th>\n",
       "      <th>行政区</th>\n",
       "      <th>观测站</th>\n",
       "      <th>气温(度)</th>\n",
       "      <th>相对湿度(%)</th>\n",
       "      <th>累积雨量(mm)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-9-1</th>\n",
       "      <td>臺南市</td>\n",
       "      <td>歸仁區</td>\n",
       "      <td>媽廟</td>\n",
       "      <td>26.2</td>\n",
       "      <td>72</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-2</th>\n",
       "      <td>雲林縣</td>\n",
       "      <td>元長鄉</td>\n",
       "      <td>元長</td>\n",
       "      <td>21.4</td>\n",
       "      <td>67</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-3</th>\n",
       "      <td>臺南市</td>\n",
       "      <td>後壁區</td>\n",
       "      <td>後壁</td>\n",
       "      <td>23.8</td>\n",
       "      <td>62</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-6</th>\n",
       "      <td>臺中市</td>\n",
       "      <td>大甲區</td>\n",
       "      <td>大甲</td>\n",
       "      <td>18.7</td>\n",
       "      <td>67</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-7</th>\n",
       "      <td>南投縣</td>\n",
       "      <td>仁愛鄉</td>\n",
       "      <td>廬山</td>\n",
       "      <td>19.4</td>\n",
       "      <td>54</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-8</th>\n",
       "      <td>屏東縣</td>\n",
       "      <td>萬巒鄉</td>\n",
       "      <td>赤山</td>\n",
       "      <td>27.5</td>\n",
       "      <td>63</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-10</th>\n",
       "      <td>金門縣</td>\n",
       "      <td>烏坵鄉</td>\n",
       "      <td>烏坵</td>\n",
       "      <td>11.2</td>\n",
       "      <td>88</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-11</th>\n",
       "      <td>臺南市</td>\n",
       "      <td>將軍區</td>\n",
       "      <td>鯤鯓國小</td>\n",
       "      <td>20.1</td>\n",
       "      <td>82</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-14</th>\n",
       "      <td>臺中市</td>\n",
       "      <td>太平區</td>\n",
       "      <td>中竹林</td>\n",
       "      <td>21</td>\n",
       "      <td>64</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-15</th>\n",
       "      <td>臺南市</td>\n",
       "      <td>安南區</td>\n",
       "      <td>安南</td>\n",
       "      <td>23.2</td>\n",
       "      <td>81</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-16</th>\n",
       "      <td>嘉義縣</td>\n",
       "      <td>民雄鄉</td>\n",
       "      <td>民雄</td>\n",
       "      <td>22.9</td>\n",
       "      <td>63</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-18</th>\n",
       "      <td>高雄市</td>\n",
       "      <td>甲仙區</td>\n",
       "      <td>甲仙</td>\n",
       "      <td>28.1</td>\n",
       "      <td>64</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-19</th>\n",
       "      <td>宜蘭縣</td>\n",
       "      <td>頭城鎮</td>\n",
       "      <td>坪林石牌</td>\n",
       "      <td>13.2</td>\n",
       "      <td>100</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-21</th>\n",
       "      <td>臺東縣</td>\n",
       "      <td>池上鄉</td>\n",
       "      <td>池上</td>\n",
       "      <td>19.1</td>\n",
       "      <td>81</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-25</th>\n",
       "      <td>臺南市</td>\n",
       "      <td>玉井區</td>\n",
       "      <td>玉井</td>\n",
       "      <td>27.7</td>\n",
       "      <td>65</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-27</th>\n",
       "      <td>臺中市</td>\n",
       "      <td>和平區</td>\n",
       "      <td>烏石坑</td>\n",
       "      <td>18.1</td>\n",
       "      <td>77</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-28</th>\n",
       "      <td>桃園市</td>\n",
       "      <td>復興區</td>\n",
       "      <td>復興</td>\n",
       "      <td>13.9</td>\n",
       "      <td>82</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-9-30</th>\n",
       "      <td>花蓮縣</td>\n",
       "      <td>秀林鄉</td>\n",
       "      <td>和中</td>\n",
       "      <td>18.4</td>\n",
       "      <td>71</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            城市  行政区   观测站 气温(度)  相对湿度(%)  累积雨量(mm)\n",
       "日期                                                \n",
       "2015-9-1   臺南市  歸仁區    媽廟  26.2       72       0.0\n",
       "2015-9-2   雲林縣  元長鄉    元長  21.4       67       0.0\n",
       "2015-9-3   臺南市  後壁區    後壁  23.8       62       0.0\n",
       "2015-9-6   臺中市  大甲區    大甲  18.7       67       0.0\n",
       "2015-9-7   南投縣  仁愛鄉    廬山  19.4       54       0.0\n",
       "2015-9-8   屏東縣  萬巒鄉    赤山  27.5       63       0.0\n",
       "2015-9-10  金門縣  烏坵鄉    烏坵  11.2       88       0.0\n",
       "2015-9-11  臺南市  將軍區  鯤鯓國小  20.1       82       0.0\n",
       "2015-9-14  臺中市  太平區   中竹林    21       64       0.0\n",
       "2015-9-15  臺南市  安南區    安南  23.2       81       0.0\n",
       "2015-9-16  嘉義縣  民雄鄉    民雄  22.9       63       0.0\n",
       "2015-9-18  高雄市  甲仙區    甲仙  28.1       64       0.0\n",
       "2015-9-19  宜蘭縣  頭城鎮  坪林石牌  13.2      100       0.0\n",
       "2015-9-21  臺東縣  池上鄉    池上  19.1       81       0.0\n",
       "2015-9-25  臺南市  玉井區    玉井  27.7       65       0.0\n",
       "2015-9-27  臺中市  和平區   烏石坑  18.1       77       0.0\n",
       "2015-9-28  桃園市  復興區    復興  13.9       82       0.0\n",
       "2015-9-30  花蓮縣  秀林鄉    和中  18.4       71       0.0"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#函数式编程的本质：\n",
    "#    函数自身可以像变量一样传递\n",
    "\n",
    "# 编写自己的函数，查询九月份累计雨量为0的数据\n",
    "def query_my_data(df):\n",
    "    return (df.index.str.startswith(\"2015-9\")) & (df[\"累积雨量(mm)\"]== 0.0)\n",
    "\n",
    "df.loc[query_my_data,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "535a685e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
